"""钉钉开放平台异常类"""

from cn_api_adapters.common.exceptions import APIError, AuthenticationError, RequestError


class DingTalkError(APIError):
    """
    钉钉开放平台基础异常类
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉API请求失败", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkError"


class DingTalkAuthError(AuthenticationError):
    """
    钉钉开放平台认证异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉开放平台认证失败", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkAuthError"


class DingTalkAccessTokenError(DingTalkAuthError):
    """
    钉钉开放平台AccessToken异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉AccessToken无效或已过期", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkAccessTokenError"


class DingTalkAppKeySecretError(DingTalkAuthError):
    """
    钉钉AppKey/AppSecret异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉AppKey或AppSecret错误", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkAppKeySecretError"


class DingTalkRateLimitError(RequestError):
    """
    钉钉开放平台限流异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
        retry_after: 建议重试时间（秒）
    """
    def __init__(self, message: str = "钉钉API请求频率超限", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None,
                 retry_after: Optional[int] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkRateLimitError"
        self.retry_after = retry_after


class DingTalkUserNotFoundError(DingTalkError):
    """
    钉钉用户不存在异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉用户不存在", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkUserNotFoundError"


class DingTalkDepartmentNotFoundError(DingTalkError):
    """
    钉钉部门不存在异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉部门不存在", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkDepartmentNotFoundError"


class DingTalkMessageError(DingTalkError):
    """
    钉钉消息发送异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉消息发送失败", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkMessageError"


class DingTalkMediaError(DingTalkError):
    """
    钉钉媒体资源异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉媒体资源处理失败", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkMediaError"


class DingTalkRobotError(DingTalkError):
    """
    钉钉机器人异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉机器人请求失败", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkRobotError"


class DingTalkRobotAuthError(DingTalkRobotError, DingTalkAuthError):
    """
    钉钉机器人认证异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉机器人认证失败", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkRobotAuthError"


class DingTalkSDKError(DingTalkError):
    """
    钉钉SDK内部异常
    
    Args:
        message: 错误消息
        error_code: 错误码
        response: API响应数据
    """
    def __init__(self, message: str = "钉钉SDK内部错误", 
                 error_code: Optional[str] = None, 
                 response: Optional[Dict[str, Any]] = None):
        super().__init__(message, error_code, response)
        self.name = "DingTalkSDKError"